//
//  HeaderCollectionViewCell.m
//  GiftNote
//
//  Created by wsh on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "HeaderCollectionViewCell.h"
#import "Screen.h"
#import "SubjectHeaderCollectionViewCell.h"
#import "INHeader.h"
#import "CommonTopicTableViewController.h"

@implementation HeaderCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //  横向滑动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 1;
        
        _collectionVC = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout];
        _collectionVC.collectionView.delegate = self;
        _collectionVC.collectionView.dataSource = self;
        _collectionVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH - 20, 120);
        _collectionVC.collectionView.backgroundColor = [UIColor whiteColor];
        _collectionVC.collectionView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:_collectionVC.view];
        [self.collectionVC.collectionView registerClass:[SubjectHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    }
    return self;
}


- (void)setDataArray:(NSMutableArray *)dataArray{
    
    _dataArray = dataArray;
    _collectionVC.collectionView.contentSize = CGSizeMake(_dataArray.count * (SCREEN_WIDTH - 20), 100);
    
    //  用来刷新界面
    [_collectionVC.collectionView reloadData];
}





#pragma mark --- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SubjectHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
    
    AttackHeaderModel *attackModel = _dataArray[indexPath.item];
    cell.model = attackModel;
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 5;
    return cell;
}


#pragma mark --- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    AttackHeaderModel *model = _dataArray[indexPath.row];
    NSInteger num = [model.headerid integerValue];
    NSString *str = [NSString stringWithFormat:kGuideTopicDetailSortURL,(long)num];
    NSString *nameStr = model.title;
    CommonTopicTableViewController *commonVC = [[CommonTopicTableViewController alloc] initWithStyle:UITableViewStylePlain];
    commonVC.urlString = str;
    commonVC.nameString = nameStr;
    commonVC.nac = self.nac;
    
    [self.nac pushViewController:commonVC animated:YES];
}



#pragma mark --- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH / 2.0, (SCREEN_WIDTH / 2.0) / 2.0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 10, 0, 10);
}


@end
